import { trim, ajax } from '../../helper/helper';
import { message } from 'antd';
import { _ } from 'underscore';

message.config({
    top: 200,
    duration: 1,
});

/////////////// 渲染列表请求
const taskListRenderData = function (dispatch, params) {
    ajax({
        type: 'POST',
        url: `/goods/list/goodsList`,
        data: {
            page: params.currentPageIndex + 1,
            size: 5,
            goodsName:params.goodsName||'',
            wxUid:"25714b2d-40c8-4d86-b8e8-c0954dc75982",
            wxSid:"wxSid", 
            orderby:"1",
            storeNo:"B02S0002",
            // miniPrice:"500",
            // maxPrice:"1000000",                                                                            
        },
        success: function (data) {
            try {
                data = JSON.parse(data);
            } catch (error) {
                
            }
            if (data.success) {
                dispatch({
                    type: 'task-list-render-data',
                    val: data.result
                });
            } else {
                dispatch({
                    type: 'task-list-render-data',
                    val: { total: 0, data: [] }
                });
            }
        }
    });
}
//// 活动action
// const AcListRenderData = function (dispatch, params) {
//     ajax({
//         type: 'POST',
//         url: `/activity/list/acList`,
//         data: {
//             storeNo:'B02S0002',
//             type:4
//         },
//         success: function (data) {
//             try {
//                 data = JSON.parse(data);
//             } catch (error) {
                
//             }
//             if (data.success) {
//                 dispatch({
//                     type: 'task-list-render-data',
//                     val: data.result
//                 });
//             } else {
//                 dispatch({
//                     type: 'task-list-render-data',
//                     val: { total: 0, data: [] }
//                 });
//             }
//         }
//     });
// }


const taskListSetPageIndex = function (dispatch, params) {
    let PageIndex = params.skip / params.limit;
    params.currentPageIndex = PageIndex;
    params.targetPageIndex = PageIndex;
    dispatch({
        type: 'task-list-set-setPageIndex',
        val: PageIndex
    })
}

const taskListRequest = function (dispatch, params) {
    dispatch({
        type: 'task-list-request',
        val: params
    })
}

//////////////// 删除商品
const deleteGoods = function (dispatch, goodsId, resolve) {
    console.log(goodsId,'dui')
    // dispatch({
    //     type: 'trigger-set-pending'
    // });
    var data={
            wxUid: '25714b2d-40c8-4d86-b8e8-c0954dc75982',
            wxSid: 'wxSid',
            storeNo: 'B02S0002',
            goodsId:goodsId
    }
    ajax({
        url: '/goods/list/delete',
        type: 'POST',
        data: data,
        dataType: 'json',
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {

            }
            if (data.success) {
                resolve();
                dispatch({
                    type: 'trigger-delete-task'
                });
                dispatch({
                    type: 'trigger-change-time'
                });
            }
            dispatch({
                type: 'trigger-set-pending'
            });
        }
    });
}


const getAllQueues = function (dispatch) {

    ajax({
        url: '/tasks/taskGroup/find',
        type: 'GET',
        data: { page: 1, size: 10 },
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {

            }
            if (data.success) {
                dispatch({
                    type: 'set-all-queues',
                    val: data.result.data
                });
            } else {
                dispatch({
                    type: 'set-all-queues',
                    val: []
                });
            }
        }
    });
}

const triggerAddQueue = function (dispatch, queue) {
    dispatch({
        type: 'trigger-add-queue'
    });
}

const taskGroupOperation = function (type, keyword, callback) {
    var url = `/tasks/taskGroup/${type}`;
    if (type === 'find') {
        ajax({
            url: url,
            type: 'GET',
            success: (data) => {
                try {
                    data = JSON.parse(data);
                } catch (error) {

                }
                if (data.success) {
                    callback(null, { options: data.result.data, complete: true });
                } else {
                    callback(null, { options: data.result.data, complete: true });
                }
            }
        });
    }


}

const getAllTaskGroups = function (type, newQueue, dispatch) {
    var url = `/tasks/taskGroup/${type}`;
    if (type === 'add' && newQueue != '') {
        ajax({
            url: url,
            type: 'GET',
            data: { taskGroupName: newQueue.name },
            success: (data) => {
                try {
                    data = JSON.parse(data);
                } catch (error) {

                }
                if (data.success) {
                    dispatch({
                        type: 'trigger-add-queue'
                    });
                    if (type === 'add') {
                        dispatch({
                            type: 'change-queue-trigger',
                            val: Date.now()
                        });
                    }
                }
            }
        });
    } else {
        ajax({
            url: url,
            type: 'GET',
            success: (data) => {
                try {
                    data = JSON.parse(data);
                } catch (error) {

                }
                if (data.success) {
                    dispatch({
                        type: 'set-all-queues',
                        val: data.result.data
                    });
                } else {
                    dispatch({
                        type: 'set-all-queues',
                        val: []
                    });
                }
            }
        });
    }

}

const commonSearch = function (type, keyword, callback) {
    // 通过关键词搜索客户、订单、执行人
    ajax({
        url: `/tasks/search/${type}`,
        type: 'GET',
        data: {
            page: 1,
            size: 10,
            keyword
        },
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {

            }
            if (data.success) {
                callback(null, { options: data.result.data, complete: true });
            } else {
                callback(null, { options: [], complete: true });
            }
        },
        error: () => {
            callback(null, { options: [], complete: true });
        }
    });
}



////// 增加确认提交按钮
const submitPics = function(dispatch, data) {
//    console.log(data,'data')
//    console.log(data.goodsUrl,'goodsUrl')
    var url=`/goods/operation/add`;
    var obj ={
            wxUid: "25714b2d-40c8-4d86-b8e8-c0954dc75982",
            wxSid: "25714b2d-40c8-4d86-b8e8-c0954dc75982",
            goodsName:data.goodsName,
            goodsUrl:data.goodsUrl,
            goodsPrice:data.goodsPrice,
            goodsDesc:data.goodsDesc,
            storeNo:"B02S0003",
            commendStatus:'',
            discountPrice:data.discountPrice,
            discountRate:'',
            findStatus:1
    }
    ajax({
        url: url,
        type: 'POST',
        data: obj,
        dataType: 'json',
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {
            
            }
            if (data.success) {
                if (url== `/goods/operation/add`)
                    message.success('添加商品成功!',3);
                else 
                    message.success('修改商品成功!',3);
                dispatch({
                    type: 'trigger-add-goods'
                });
                dispatch({
                    type: 'trigger-change-time'
                });
            } else {
                message.error('请再次点击添加!',3);
            }
            dispatch({
                type: 'trigger-set-pending'
            });
        }
        
    });
}
///// 修改确认提交按钮
const updateGoods=function(dispatch, obj){
    console.log(obj.goodsDesc1,'goodsUrl1')
    console.log(obj.goodsUrl1,'goodsUrl1')
    var postJson ={
            wxUid: "25714b2d-40c8-4d86-b8e8-c0954dc75982",
            wxSid: "25714b2d-40c8-4d86-b8e8-c0954dc75982",
            goodsName:obj.goodsName,
            goodsId:obj.goodsId,
            goodsUrl:obj.goodsUrl1||obj.goodsUrl,
            goodsPrice:obj.goodsPrice,
            goodsDesc:obj.goodsDesc1||obj.goodsDesc,
            storeNo:"B02S0002",
            commendStatus:'',
            discountPrice:obj.discountPrice,
            discountRate:obj.discountRate||'',
            findStatus:1,
            soldout:''
    }
    ajax({
        url:`/goods/operation/update`,
        type: 'POST',
        data: postJson,
        dataType: 'json',
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {
                    
            }
            if (data.success) {
                message.success('修改商品成功!',3);
                dispatch({
                    type: 'trigger-add-goods'
                });
                dispatch({
                    type: 'trigger-change-time'
                });
            } else {
                message.error('请再次点击保存!',3);
            }
            dispatch({
                type: 'trigger-set-pending'
            });
        }
    });
}

//////////////////// 下架商品
const soldoutGoods=function(dispatch, obj){
    var postJson ={
            wxUid: "25714b2d-40c8-4d86-b8e8-c0954dc75982",
            wxSid: "25714b2d-40c8-4d86-b8e8-c0954dc75982",
            goodsName:obj.goodsName,
            goodsId:obj.goodsId,
            goodsUrl:obj.goodsUrl,
            goodsPrice:obj.goodsPrice,
            goodsDesc:obj.goodsDesc,
            storeNo:"B02S0002",
            commendStatus:'',
            discountPrice:obj.discountPrice,
            discountRate:obj.discountRate||'',
            findStatus:1,
            soldout:1
    }
    ajax({
        url:`/goods/operation/update`,
        type: 'POST',
        data: postJson,
        dataType: 'json',
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {
                    
            }
            if (data.success) {
                message.success('下架商品成功!',3);
                // dispatch({
                //     type: 'trigger-add-goods'
                // });
                dispatch({
                    type: 'trigger-change-time'
                });
            } else {
                message.error('请再次点击保存!',3);
            }
            dispatch({
                type: 'trigger-set-pending'
            });
        }
    });
}

export const actions = {
    taskListRenderData: taskListRenderData,
    taskListSetPageIndex: taskListSetPageIndex,
    taskListRequest: taskListRequest,
    // createNewTask: createNewTask,
    deleteGoods: deleteGoods,
    getAllQueues: getAllQueues,
    triggerAddQueue: triggerAddQueue,
    // commonSearch,
    taskGroupOperation: taskGroupOperation,
    getAllTaskGroups: getAllTaskGroups,
    submitPics,
    updateGoods,
    soldoutGoods,
}